// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <cantor/parser/parser.h>
#include <cantor/transform/functions/func_expr.h>
#include <cantor/transform/expr.h>
#include <cantor/transform/exprs/when_clause.h>
#include <cantor/transform/dqls/indirection.h>

namespace cantor::transform {

    void TransFuncExpr::resolve_ast(const cantor::parser::FuncExpr *node) {
        if (!node) {
            status = turbo::data_loss_error("no expr");
            return;
        }

        /// fn_name
        func_expr.set_fn_name(node->fn_name.to_string());
        /// func_type
        func_expr.set_func_type(node->func_type);
        /// is_not
        func_expr.set_is_not(node->is_not);
        /// distinct
        func_expr.set_distinct(node->distinct);
        /// is_star
        func_expr.set_is_star(node->is_star);
        /// exprs
        auto  exprs = func_expr.mutable_exprs();
        exprs->Reserve(node->exprs.size());
        for (size_t i = 0; i < node->exprs.size(); ++i) {
            auto expr = exprs->Add();
            status = TransExpr::resolve_to(node->exprs[i], expr);
            if (!status.ok()) {
                return;
            }
        }

        if (node->indirections.size() >= 1) {
            func_expr.mutable_indirections()->Reserve(node->indirections.size());
            for (auto i = 0; i < node->indirections.size(); ++i) {
                auto ind = func_expr.mutable_indirections()->Add();
                TransIndirection trans;
                trans.resolve_ast(node->indirections[i]);
                auto &s = trans.check();
                if (!s.ok()) {
                    status = s;
                    return;
                }
                *ind = std::move(trans.indirection);
            }
        }

        set_proto_location(node, func_expr.mutable_location());
        set_proto_status(func_expr.mutable_status());
    }

    const turbo::Status &TransFuncExpr::check() {
        if (!status.ok()) {
            return status;
        }
        if (!func_expr.has_location()) {
            make_missing_required_field("location");
            return status;
        }
        return status;
    }
    turbo::Status TransFuncExpr::resolve_to(turbo::Nonnull<const cantor::parser::FuncExpr *> arg,
                                      turbo::Nonnull<pb::ast::FuncExpr *> out) {
        TransFuncExpr trans;
        trans.resolve_ast(arg);
        auto status = trans.check();
        if (!status.ok()) {
            return status;
        }
        *out = std::move(trans.func_expr);
        return status;
    }
} // namespace cantor::transform
